<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html 
     PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
  <title>Class: ActiveRecord::Errors</title>
  <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
  <meta http-equiv="Content-Script-Type" content="text/javascript" />
  <link rel="stylesheet" href="../.././rdoc-style.css" type="text/css" media="screen" />
  <script type="text/javascript">
  // <![CDATA[

  function popupCode( url ) {
    window.open(url, "Code", "resizable=yes,scrollbars=yes,toolbar=no,status=no,height=150,width=400")
  }

  function toggleCode( id ) {
    if ( document.getElementById )
      elem = document.getElementById( id );
    else if ( document.all )
      elem = eval( "document.all." + id );
    else
      return false;

    elemStyle = elem.style;
    
    if ( elemStyle.display != "block" ) {
      elemStyle.display = "block"
    } else {
      elemStyle.display = "none"
    }

    return true;
  }
  
  // Make codeblocks hidden by default
  document.writeln( "<style type=\"text/css\">div.method-source-code { display: none }</style>" )
  
  // ]]>
  </script>

</head>
<body>



    <div id="classHeader">
        <table class="header-table">
        <tr class="top-aligned-row">
          <td><strong>Class</strong></td>
          <td class="class-name-in-header">ActiveRecord::Errors</td>
        </tr>
        <tr class="top-aligned-row">
            <td><strong>In:</strong></td>
            <td>
                <a href="../../files/vendor/rails/activerecord/lib/active_record/validations_rb.html">
                vendor/rails/activerecord/lib/active_record/validations.rb
                </a>
        <br />
            </td>
        </tr>

        <tr class="top-aligned-row">
            <td><strong>Parent:</strong></td>
            <td>
                <a href="../Object.html">
                Object
               </a>
            </td>
        </tr>
        </table>
    </div>
  <!-- banner header -->

  <div id="bodyContent">



  <div id="contextContent">

    <div id="description">
      <p>
Active Record validation is reported to and from this object, which is used
by <a href="Base.html#M001739">Base#save</a> to determine whether the
object is in a valid state to be saved. See usage example in <a
href="Validations.html">Validations</a>.
</p>

    </div>


   </div>

    <div id="method-list">
      <h3 class="section-bar">Methods</h3>

      <div class="name-list">
      <a href="#M001643">[]</a>&nbsp;&nbsp;
      <a href="#M001637">add</a>&nbsp;&nbsp;
      <a href="#M001639">add_on_blank</a>&nbsp;&nbsp;
      <a href="#M001638">add_on_empty</a>&nbsp;&nbsp;
      <a href="#M001636">add_to_base</a>&nbsp;&nbsp;
      <a href="#M001649">clear</a>&nbsp;&nbsp;
      <a href="#M001651">count</a>&nbsp;&nbsp;
      <a href="#M001635">default_error_messages</a>&nbsp;&nbsp;
      <a href="#M001645">each</a>&nbsp;&nbsp;
      <a href="#M001646">each_full</a>&nbsp;&nbsp;
      <a href="#M001648">empty?</a>&nbsp;&nbsp;
      <a href="#M001647">full_messages</a>&nbsp;&nbsp;
      <a href="#M001640">generate_message</a>&nbsp;&nbsp;
      <a href="#M001641">invalid?</a>&nbsp;&nbsp;
      <a href="#M001652">length</a>&nbsp;&nbsp;
      <a href="#M001642">on</a>&nbsp;&nbsp;
      <a href="#M001644">on_base</a>&nbsp;&nbsp;
      <a href="#M001650">size</a>&nbsp;&nbsp;
      <a href="#M001653">to_xml</a>&nbsp;&nbsp;
      </div>
    </div>

  </div>


    <!-- if includes -->
    <div id="includes">
      <h3 class="section-bar">Included Modules</h3>

      <div id="includes-list">
        <span class="include-name"><a href="../Enumerable.html">Enumerable</a></span>
      </div>
    </div>

    <div id="section">





      


    <!-- if method_list -->
    <div id="methods">
      <h3 class="section-bar">Public Class methods</h3>

      <div id="method-M001635" class="method-detail">
        <a name="M001635"></a>

        <div class="method-heading">
          <a href="#M001635" class="method-signature">
          <span class="method-name">default_error_messages</span><span class="method-args">()</span>
          </a>
        </div>
      
        <div class="method-description">
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M001635-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M001635-source">
<pre>
    <span class="ruby-comment cmt"># File vendor/rails/activerecord/lib/active_record/validations.rb, line 23</span>
23:       <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">default_error_messages</span>
24:         <span class="ruby-constant">ActiveSupport</span><span class="ruby-operator">::</span><span class="ruby-constant">Deprecation</span>.<span class="ruby-identifier">warn</span>(<span class="ruby-value str">&quot;ActiveRecord::Errors.default_error_messages has been deprecated. Please use I18n.translate('activerecord.errors.messages').&quot;</span>)
25:         <span class="ruby-constant">I18n</span>.<span class="ruby-identifier">translate</span> <span class="ruby-value str">'activerecord.errors.messages'</span>
26:       <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>

      <h3 class="section-bar">Public Instance methods</h3>

      <div id="method-M001643" class="method-detail">
        <a name="M001643"></a>

        <div class="method-heading">
          <span class="method-name">[]</span><span class="method-args">(attribute)</span>
        </div>
      
        <div class="method-description">
          <p>
Alias for <a href="Errors.html#M001642">on</a>
</p>
        </div>
      </div>

      <div id="method-M001637" class="method-detail">
        <a name="M001637"></a>

        <div class="method-heading">
          <a href="#M001637" class="method-signature">
          <span class="method-name">add</span><span class="method-args">(attribute, message = nil, options = {})</span>
          </a>
        </div>
      
        <div class="method-description">
          <p>
Adds an error message (<tt>messsage</tt>) to the <tt>attribute</tt>, which
will be returned <a href="Errors.html#M001642">on</a> a call to <tt><a
href="Errors.html#M001642">on</a>(attribute)</tt> for the same attribute
and ensure that this error object returns false when asked if <tt><a
href="Errors.html#M001648">empty?</a></tt>. More than one error can be
added to the same <tt>attribute</tt> in which case an array will be
returned <a href="Errors.html#M001642">on</a> a call to <tt><a
href="Errors.html#M001642">on</a>(attribute)</tt>. If no <tt>messsage</tt>
is supplied, :invalid is assumed. If <tt>message</tt> is a Symbol, it will
be translated, using the appropriate scope (see translate_error).
</p>
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M001637-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M001637-source">
<pre>
    <span class="ruby-comment cmt"># File vendor/rails/activerecord/lib/active_record/validations.rb, line 46</span>
46:     <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">add</span>(<span class="ruby-identifier">attribute</span>, <span class="ruby-identifier">message</span> = <span class="ruby-keyword kw">nil</span>, <span class="ruby-identifier">options</span> = {})
47:       <span class="ruby-identifier">message</span> <span class="ruby-operator">||=</span> <span class="ruby-identifier">:invalid</span>
48:       <span class="ruby-identifier">message</span> = <span class="ruby-identifier">generate_message</span>(<span class="ruby-identifier">attribute</span>, <span class="ruby-identifier">message</span>, <span class="ruby-identifier">options</span>) <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">message</span>.<span class="ruby-identifier">is_a?</span>(<span class="ruby-constant">Symbol</span>)
49:       <span class="ruby-ivar">@errors</span>[<span class="ruby-identifier">attribute</span>.<span class="ruby-identifier">to_s</span>] <span class="ruby-operator">||=</span> []
50:       <span class="ruby-ivar">@errors</span>[<span class="ruby-identifier">attribute</span>.<span class="ruby-identifier">to_s</span>] <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-identifier">message</span>
51:     <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>

      <div id="method-M001639" class="method-detail">
        <a name="M001639"></a>

        <div class="method-heading">
          <a href="#M001639" class="method-signature">
          <span class="method-name">add_on_blank</span><span class="method-args">(attributes, custom_message = nil)</span>
          </a>
        </div>
      
        <div class="method-description">
          <p>
Will <a href="Errors.html#M001637">add</a> an error message to <a
href="Errors.html#M001645">each</a> of the attributes in
<tt>attributes</tt> that is blank (using Object#blank?).
</p>
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M001639-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M001639-source">
<pre>
    <span class="ruby-comment cmt"># File vendor/rails/activerecord/lib/active_record/validations.rb, line 63</span>
63:     <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">add_on_blank</span>(<span class="ruby-identifier">attributes</span>, <span class="ruby-identifier">custom_message</span> = <span class="ruby-keyword kw">nil</span>)
64:       <span class="ruby-keyword kw">for</span> <span class="ruby-identifier">attr</span> <span class="ruby-keyword kw">in</span> [<span class="ruby-identifier">attributes</span>].<span class="ruby-identifier">flatten</span>
65:         <span class="ruby-identifier">value</span> = <span class="ruby-ivar">@base</span>.<span class="ruby-identifier">respond_to?</span>(<span class="ruby-identifier">attr</span>.<span class="ruby-identifier">to_s</span>) <span class="ruby-operator">?</span> <span class="ruby-ivar">@base</span>.<span class="ruby-identifier">send</span>(<span class="ruby-identifier">attr</span>.<span class="ruby-identifier">to_s</span>) <span class="ruby-operator">:</span> <span class="ruby-ivar">@base</span>[<span class="ruby-identifier">attr</span>.<span class="ruby-identifier">to_s</span>]
66:         <span class="ruby-identifier">add</span>(<span class="ruby-identifier">attr</span>, <span class="ruby-identifier">:blank</span>, <span class="ruby-identifier">:default</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-identifier">custom_message</span>) <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">value</span>.<span class="ruby-identifier">blank?</span>
67:       <span class="ruby-keyword kw">end</span>
68:     <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>

      <div id="method-M001638" class="method-detail">
        <a name="M001638"></a>

        <div class="method-heading">
          <a href="#M001638" class="method-signature">
          <span class="method-name">add_on_empty</span><span class="method-args">(attributes, custom_message = nil)</span>
          </a>
        </div>
      
        <div class="method-description">
          <p>
Will <a href="Errors.html#M001637">add</a> an error message to <a
href="Errors.html#M001645">each</a> of the attributes in
<tt>attributes</tt> that is empty.
</p>
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M001638-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M001638-source">
<pre>
    <span class="ruby-comment cmt"># File vendor/rails/activerecord/lib/active_record/validations.rb, line 54</span>
54:     <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">add_on_empty</span>(<span class="ruby-identifier">attributes</span>, <span class="ruby-identifier">custom_message</span> = <span class="ruby-keyword kw">nil</span>)
55:       <span class="ruby-keyword kw">for</span> <span class="ruby-identifier">attr</span> <span class="ruby-keyword kw">in</span> [<span class="ruby-identifier">attributes</span>].<span class="ruby-identifier">flatten</span>
56:         <span class="ruby-identifier">value</span> = <span class="ruby-ivar">@base</span>.<span class="ruby-identifier">respond_to?</span>(<span class="ruby-identifier">attr</span>.<span class="ruby-identifier">to_s</span>) <span class="ruby-operator">?</span> <span class="ruby-ivar">@base</span>.<span class="ruby-identifier">send</span>(<span class="ruby-identifier">attr</span>.<span class="ruby-identifier">to_s</span>) <span class="ruby-operator">:</span> <span class="ruby-ivar">@base</span>[<span class="ruby-identifier">attr</span>.<span class="ruby-identifier">to_s</span>]
57:         <span class="ruby-identifier">is_empty</span> = <span class="ruby-identifier">value</span>.<span class="ruby-identifier">respond_to?</span>(<span class="ruby-identifier">:empty?</span>) <span class="ruby-operator">?</span> <span class="ruby-identifier">value</span>.<span class="ruby-identifier">empty?</span> <span class="ruby-operator">:</span> <span class="ruby-keyword kw">false</span>
58:         <span class="ruby-identifier">add</span>(<span class="ruby-identifier">attr</span>, <span class="ruby-identifier">:empty</span>, <span class="ruby-identifier">:default</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-identifier">custom_message</span>) <span class="ruby-keyword kw">unless</span> <span class="ruby-operator">!</span><span class="ruby-identifier">value</span>.<span class="ruby-identifier">nil?</span> <span class="ruby-operator">&amp;&amp;</span> <span class="ruby-operator">!</span><span class="ruby-identifier">is_empty</span>
59:       <span class="ruby-keyword kw">end</span>
60:     <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>

      <div id="method-M001636" class="method-detail">
        <a name="M001636"></a>

        <div class="method-heading">
          <a href="#M001636" class="method-signature">
          <span class="method-name">add_to_base</span><span class="method-args">(msg)</span>
          </a>
        </div>
      
        <div class="method-description">
          <p>
Adds an error to the base object instead of any particular attribute. This
is used to report errors that don&#8216;t tie to any specific attribute,
but rather to the object as a whole. These error messages don&#8216;t get
prepended with any field name when iterating with <tt><a
href="Errors.html#M001646">each_full</a></tt>, so they should be complete
sentences.
</p>
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M001636-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M001636-source">
<pre>
    <span class="ruby-comment cmt"># File vendor/rails/activerecord/lib/active_record/validations.rb, line 37</span>
37:     <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">add_to_base</span>(<span class="ruby-identifier">msg</span>)
38:       <span class="ruby-identifier">add</span>(<span class="ruby-identifier">:base</span>, <span class="ruby-identifier">msg</span>)
39:     <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>

      <div id="method-M001649" class="method-detail">
        <a name="M001649"></a>

        <div class="method-heading">
          <a href="#M001649" class="method-signature">
          <span class="method-name">clear</span><span class="method-args">()</span>
          </a>
        </div>
      
        <div class="method-description">
          <p>
Removes all errors that have been added.
</p>
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M001649-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M001649-source">
<pre>
     <span class="ruby-comment cmt"># File vendor/rails/activerecord/lib/active_record/validations.rb, line 221</span>
221:     <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">clear</span>
222:       <span class="ruby-ivar">@errors</span> = {}
223:     <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>

      <div id="method-M001651" class="method-detail">
        <a name="M001651"></a>

        <div class="method-heading">
          <span class="method-name">count</span><span class="method-args">()</span>
        </div>
      
        <div class="method-description">
          <p>
Alias for <a href="Errors.html#M001650">size</a>
</p>
        </div>
      </div>

      <div id="method-M001645" class="method-detail">
        <a name="M001645"></a>

        <div class="method-heading">
          <a href="#M001645" class="method-signature">
          <span class="method-name">each</span><span class="method-args">() {|attr, msg| ...}</span>
          </a>
        </div>
      
        <div class="method-description">
          <p>
Yields <a href="Errors.html#M001645">each</a> attribute and associated
message per error added.
</p>
<pre>
  class Company &lt; ActiveRecord::Base
    validates_presence_of :name, :address, :email
    validates_length_of :name, :in =&gt; 5..30
  end

  company = Company.create(:address =&gt; '123 First St.')
  company.errors.each{|attr,msg| puts &quot;#{attr} - #{msg}&quot; }
  # =&gt; name - is too short (minimum is 5 characters)
  #    name - can't be blank
  #    address - can't be blank
</pre>
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M001645-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M001645-source">
<pre>
     <span class="ruby-comment cmt"># File vendor/rails/activerecord/lib/active_record/validations.rb, line 165</span>
165:     <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">each</span>
166:       <span class="ruby-ivar">@errors</span>.<span class="ruby-identifier">each_key</span> { <span class="ruby-operator">|</span><span class="ruby-identifier">attr</span><span class="ruby-operator">|</span> <span class="ruby-ivar">@errors</span>[<span class="ruby-identifier">attr</span>].<span class="ruby-identifier">each</span> { <span class="ruby-operator">|</span><span class="ruby-identifier">msg</span><span class="ruby-operator">|</span> <span class="ruby-keyword kw">yield</span> <span class="ruby-identifier">attr</span>, <span class="ruby-identifier">msg</span> } }
167:     <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>

      <div id="method-M001646" class="method-detail">
        <a name="M001646"></a>

        <div class="method-heading">
          <a href="#M001646" class="method-signature">
          <span class="method-name">each_full</span><span class="method-args">() {|msg| ...}</span>
          </a>
        </div>
      
        <div class="method-description">
          <p>
Yields <a href="Errors.html#M001645">each</a> full error message added. So
<tt>Person.errors.<a
href="Errors.html#M001637">add</a>(&quot;first_name&quot;,
&quot;can&#8216;t be empty&quot;)</tt> will be returned through iteration
as &quot;First name can&#8216;t be empty&quot;.
</p>
<pre>
  class Company &lt; ActiveRecord::Base
    validates_presence_of :name, :address, :email
    validates_length_of :name, :in =&gt; 5..30
  end

  company = Company.create(:address =&gt; '123 First St.')
  company.errors.each_full{|msg| puts msg }
  # =&gt; Name is too short (minimum is 5 characters)
  #    Name can't be blank
  #    Address can't be blank
</pre>
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M001646-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M001646-source">
<pre>
     <span class="ruby-comment cmt"># File vendor/rails/activerecord/lib/active_record/validations.rb, line 182</span>
182:     <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">each_full</span>
183:       <span class="ruby-identifier">full_messages</span>.<span class="ruby-identifier">each</span> { <span class="ruby-operator">|</span><span class="ruby-identifier">msg</span><span class="ruby-operator">|</span> <span class="ruby-keyword kw">yield</span> <span class="ruby-identifier">msg</span> }
184:     <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>

      <div id="method-M001648" class="method-detail">
        <a name="M001648"></a>

        <div class="method-heading">
          <a href="#M001648" class="method-signature">
          <span class="method-name">empty?</span><span class="method-args">()</span>
          </a>
        </div>
      
        <div class="method-description">
          <p>
Returns true if no errors have been added.
</p>
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M001648-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M001648-source">
<pre>
     <span class="ruby-comment cmt"># File vendor/rails/activerecord/lib/active_record/validations.rb, line 216</span>
216:     <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">empty?</span>
217:       <span class="ruby-ivar">@errors</span>.<span class="ruby-identifier">empty?</span>
218:     <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>

      <div id="method-M001647" class="method-detail">
        <a name="M001647"></a>

        <div class="method-heading">
          <a href="#M001647" class="method-signature">
          <span class="method-name">full_messages</span><span class="method-args">(options = {})</span>
          </a>
        </div>
      
        <div class="method-description">
          <p>
Returns all the full error messages in an array.
</p>
<pre>
  class Company &lt; ActiveRecord::Base
    validates_presence_of :name, :address, :email
    validates_length_of :name, :in =&gt; 5..30
  end

  company = Company.create(:address =&gt; '123 First St.')
  company.errors.full_messages # =&gt;
    [&quot;Name is too short (minimum is 5 characters)&quot;, &quot;Name can't be blank&quot;, &quot;Address can't be blank&quot;]
</pre>
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M001647-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M001647-source">
<pre>
     <span class="ruby-comment cmt"># File vendor/rails/activerecord/lib/active_record/validations.rb, line 196</span>
196:     <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">full_messages</span>(<span class="ruby-identifier">options</span> = {})
197:       <span class="ruby-identifier">full_messages</span> = []
198: 
199:       <span class="ruby-ivar">@errors</span>.<span class="ruby-identifier">each_key</span> <span class="ruby-keyword kw">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">attr</span><span class="ruby-operator">|</span>
200:         <span class="ruby-ivar">@errors</span>[<span class="ruby-identifier">attr</span>].<span class="ruby-identifier">each</span> <span class="ruby-keyword kw">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">message</span><span class="ruby-operator">|</span>
201:           <span class="ruby-keyword kw">next</span> <span class="ruby-keyword kw">unless</span> <span class="ruby-identifier">message</span>
202: 
203:           <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">attr</span> <span class="ruby-operator">==</span> <span class="ruby-value str">&quot;base&quot;</span>
204:             <span class="ruby-identifier">full_messages</span> <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-identifier">message</span>
205:           <span class="ruby-keyword kw">else</span>
206:             <span class="ruby-comment cmt">#key = :&quot;activerecord.att.#{@base.class.name.underscore.to_sym}.#{attr}&quot; </span>
207:             <span class="ruby-identifier">attr_name</span> = <span class="ruby-ivar">@base</span>.<span class="ruby-identifier">class</span>.<span class="ruby-identifier">human_attribute_name</span>(<span class="ruby-identifier">attr</span>)
208:             <span class="ruby-identifier">full_messages</span> <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-identifier">attr_name</span> <span class="ruby-operator">+</span> <span class="ruby-value str">' '</span> <span class="ruby-operator">+</span> <span class="ruby-identifier">message</span>
209:           <span class="ruby-keyword kw">end</span>
210:         <span class="ruby-keyword kw">end</span>
211:       <span class="ruby-keyword kw">end</span>
212:       <span class="ruby-identifier">full_messages</span>
213:     <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>

      <div id="method-M001640" class="method-detail">
        <a name="M001640"></a>

        <div class="method-heading">
          <a href="#M001640" class="method-signature">
          <span class="method-name">generate_message</span><span class="method-args">(attribute, message = :invalid, options = {})</span>
          </a>
        </div>
      
        <div class="method-description">
          <p>
Translates an error message in it&#8216;s default scope
(<tt>activerecord.errrors.messages</tt>). Error messages are first looked
up in <tt>models.MODEL.attributes.ATTRIBUTE.MESSAGE</tt>, if it&#8216;s not
there, it&#8216;s looked up in <tt>models.MODEL.MESSAGE</tt> and if that is
not there it returns the translation of the default message (e.g.
<tt>activerecord.errors.messages.MESSAGE</tt>). The translated model name,
translated attribute name and the value are available for interpolation.
</p>
<p>
When using inheritence in your models, it will check all the inherited
models too, but only if the model itself hasn&#8216;t been found. Say you
have <tt>class Admin &lt; User; end</tt> and you wanted the translation for
the <tt>:blank</tt> error <tt>message</tt> for the <tt>title</tt>
<tt>attribute</tt>, it looks for these translations:
</p>
<p>
&lt;ol&gt;
&lt;li&gt;<tt>activerecord.errors.models.admin.attributes.title.blank</tt>&lt;/li&gt;
&lt;li&gt;<tt>activerecord.errors.models.admin.blank</tt>&lt;/li&gt;
&lt;li&gt;<tt>activerecord.errors.models.user.attributes.title.blank</tt>&lt;/li&gt;
&lt;li&gt;<tt>activerecord.errors.models.user.blank</tt>&lt;/li&gt;
&lt;li&gt;<tt>activerecord.errors.messages.blank</tt>&lt;/li&gt;
&lt;li&gt;any default you provided through the <tt>options</tt> hash (in
the activerecord.errors scope)&lt;/li&gt; &lt;/ol&gt;
</p>
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M001640-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M001640-source">
<pre>
     <span class="ruby-comment cmt"># File vendor/rails/activerecord/lib/active_record/validations.rb, line 88</span>
 88:     <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">generate_message</span>(<span class="ruby-identifier">attribute</span>, <span class="ruby-identifier">message</span> = <span class="ruby-identifier">:invalid</span>, <span class="ruby-identifier">options</span> = {})
 89: 
 90:       <span class="ruby-identifier">message</span>, <span class="ruby-identifier">options</span>[<span class="ruby-identifier">:default</span>] = <span class="ruby-identifier">options</span>[<span class="ruby-identifier">:default</span>], <span class="ruby-identifier">message</span> <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">options</span>[<span class="ruby-identifier">:default</span>].<span class="ruby-identifier">is_a?</span>(<span class="ruby-constant">Symbol</span>)
 91: 
 92:       <span class="ruby-identifier">defaults</span> = <span class="ruby-ivar">@base</span>.<span class="ruby-identifier">class</span>.<span class="ruby-identifier">self_and_descendents_from_active_record</span>.<span class="ruby-identifier">map</span> <span class="ruby-keyword kw">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">klass</span><span class="ruby-operator">|</span> 
 93:         [ <span class="ruby-node">&quot;models.#{klass.name.underscore}.attributes.#{attribute}.#{message}&quot;</span><span class="ruby-node">&quot;models.#{klass.name.underscore}.attributes.#{attribute}.#{message}&quot;</span>, 
 94:           <span class="ruby-node">&quot;models.#{klass.name.underscore}.#{message}&quot;</span><span class="ruby-node">&quot;models.#{klass.name.underscore}.#{message}&quot;</span> ]
 95:       <span class="ruby-keyword kw">end</span>
 96:       
 97:       <span class="ruby-identifier">defaults</span> <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-identifier">options</span>.<span class="ruby-identifier">delete</span>(<span class="ruby-identifier">:default</span>)
 98:       <span class="ruby-identifier">defaults</span> = <span class="ruby-identifier">defaults</span>.<span class="ruby-identifier">compact</span>.<span class="ruby-identifier">flatten</span> <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-node">&quot;messages.#{message}&quot;</span><span class="ruby-node">&quot;messages.#{message}&quot;</span>
 99: 
100:       <span class="ruby-identifier">key</span> = <span class="ruby-identifier">defaults</span>.<span class="ruby-identifier">shift</span>
101:       <span class="ruby-identifier">value</span> = <span class="ruby-ivar">@base</span>.<span class="ruby-identifier">respond_to?</span>(<span class="ruby-identifier">attribute</span>) <span class="ruby-operator">?</span> <span class="ruby-ivar">@base</span>.<span class="ruby-identifier">send</span>(<span class="ruby-identifier">attribute</span>) <span class="ruby-operator">:</span> <span class="ruby-keyword kw">nil</span>
102: 
103:       <span class="ruby-identifier">options</span> = { <span class="ruby-identifier">:default</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-identifier">defaults</span>,
104:         <span class="ruby-identifier">:model</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-ivar">@base</span>.<span class="ruby-identifier">class</span>.<span class="ruby-identifier">human_name</span>,
105:         <span class="ruby-identifier">:attribute</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-ivar">@base</span>.<span class="ruby-identifier">class</span>.<span class="ruby-identifier">human_attribute_name</span>(<span class="ruby-identifier">attribute</span>.<span class="ruby-identifier">to_s</span>),
106:         <span class="ruby-identifier">:value</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-identifier">value</span>,
107:         <span class="ruby-identifier">:scope</span> =<span class="ruby-operator">&gt;</span> [<span class="ruby-identifier">:activerecord</span>, <span class="ruby-identifier">:errors</span>]
108:       }.<span class="ruby-identifier">merge</span>(<span class="ruby-identifier">options</span>)
109: 
110:       <span class="ruby-constant">I18n</span>.<span class="ruby-identifier">translate</span>(<span class="ruby-identifier">key</span>, <span class="ruby-identifier">options</span>)
111:     <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>

      <div id="method-M001641" class="method-detail">
        <a name="M001641"></a>

        <div class="method-heading">
          <a href="#M001641" class="method-signature">
          <span class="method-name">invalid?</span><span class="method-args">(attribute)</span>
          </a>
        </div>
      
        <div class="method-description">
          <p>
Returns true if the specified <tt>attribute</tt> has errors associated with
it.
</p>
<pre>
  class Company &lt; ActiveRecord::Base
    validates_presence_of :name, :address, :email
    validates_length_of :name, :in =&gt; 5..30
  end

  company = Company.create(:address =&gt; '123 First St.')
  company.errors.invalid?(:name)      # =&gt; true
  company.errors.invalid?(:address)   # =&gt; false
</pre>
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M001641-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M001641-source">
<pre>
     <span class="ruby-comment cmt"># File vendor/rails/activerecord/lib/active_record/validations.rb, line 123</span>
123:     <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">invalid?</span>(<span class="ruby-identifier">attribute</span>)
124:       <span class="ruby-operator">!</span><span class="ruby-ivar">@errors</span>[<span class="ruby-identifier">attribute</span>.<span class="ruby-identifier">to_s</span>].<span class="ruby-identifier">nil?</span>
125:     <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>

      <div id="method-M001652" class="method-detail">
        <a name="M001652"></a>

        <div class="method-heading">
          <span class="method-name">length</span><span class="method-args">()</span>
        </div>
      
        <div class="method-description">
          <p>
Alias for <a href="Errors.html#M001650">size</a>
</p>
        </div>
      </div>

      <div id="method-M001642" class="method-detail">
        <a name="M001642"></a>

        <div class="method-heading">
          <a href="#M001642" class="method-signature">
          <span class="method-name">on</span><span class="method-args">(attribute)</span>
          </a>
        </div>
      
        <div class="method-description">
          <p>
Returns <tt>nil</tt>, if no errors are associated with the specified
<tt>attribute</tt>. Returns the error message, if one error is associated
with the specified <tt>attribute</tt>. Returns an array of error messages,
if more than one error is associated with the specified <tt>attribute</tt>.
</p>
<pre>
  class Company &lt; ActiveRecord::Base
    validates_presence_of :name, :address, :email
    validates_length_of :name, :in =&gt; 5..30
  end

  company = Company.create(:address =&gt; '123 First St.')
  company.errors.on(:name)      # =&gt; [&quot;is too short (minimum is 5 characters)&quot;, &quot;can't be blank&quot;]
  company.errors.on(:email)     # =&gt; &quot;can't be blank&quot;
  company.errors.on(:address)   # =&gt; nil
</pre>
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M001642-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M001642-source">
<pre>
     <span class="ruby-comment cmt"># File vendor/rails/activerecord/lib/active_record/validations.rb, line 140</span>
140:     <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">on</span>(<span class="ruby-identifier">attribute</span>)
141:       <span class="ruby-identifier">errors</span> = <span class="ruby-ivar">@errors</span>[<span class="ruby-identifier">attribute</span>.<span class="ruby-identifier">to_s</span>]
142:       <span class="ruby-keyword kw">return</span> <span class="ruby-keyword kw">nil</span> <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">errors</span>.<span class="ruby-identifier">nil?</span>
143:       <span class="ruby-identifier">errors</span>.<span class="ruby-identifier">size</span> <span class="ruby-operator">==</span> <span class="ruby-value">1</span> <span class="ruby-operator">?</span> <span class="ruby-identifier">errors</span>.<span class="ruby-identifier">first</span> <span class="ruby-operator">:</span> <span class="ruby-identifier">errors</span>
144:     <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>

      <div id="method-M001644" class="method-detail">
        <a name="M001644"></a>

        <div class="method-heading">
          <a href="#M001644" class="method-signature">
          <span class="method-name">on_base</span><span class="method-args">()</span>
          </a>
        </div>
      
        <div class="method-description">
          <p>
Returns errors assigned to the base object through <tt><a
href="Errors.html#M001636">add_to_base</a></tt> according to the normal
rules of <tt><a href="Errors.html#M001642">on</a>(attribute)</tt>.
</p>
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M001644-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M001644-source">
<pre>
     <span class="ruby-comment cmt"># File vendor/rails/activerecord/lib/active_record/validations.rb, line 149</span>
149:     <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">on_base</span>
150:       <span class="ruby-identifier">on</span>(<span class="ruby-identifier">:base</span>)
151:     <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>

      <div id="method-M001650" class="method-detail">
        <a name="M001650"></a>

        <div class="method-heading">
          <a href="#M001650" class="method-signature">
          <span class="method-name">size</span><span class="method-args">()</span>
          </a>
        </div>
      
        <div class="method-description">
          <p>
Returns the total number of errors added. Two errors added to the same
attribute will be counted as such.
</p>
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M001650-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M001650-source">
<pre>
     <span class="ruby-comment cmt"># File vendor/rails/activerecord/lib/active_record/validations.rb, line 226</span>
226:     <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">size</span>
227:       <span class="ruby-ivar">@errors</span>.<span class="ruby-identifier">values</span>.<span class="ruby-identifier">inject</span>(<span class="ruby-value">0</span>) { <span class="ruby-operator">|</span><span class="ruby-identifier">error_count</span>, <span class="ruby-identifier">attribute</span><span class="ruby-operator">|</span> <span class="ruby-identifier">error_count</span> <span class="ruby-operator">+</span> <span class="ruby-identifier">attribute</span>.<span class="ruby-identifier">size</span> }
228:     <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>

      <div id="method-M001653" class="method-detail">
        <a name="M001653"></a>

        <div class="method-heading">
          <a href="#M001653" class="method-signature">
          <span class="method-name">to_xml</span><span class="method-args">(options={})</span>
          </a>
        </div>
      
        <div class="method-description">
          <p>
Returns an XML representation of this error object.
</p>
<pre>
  class Company &lt; ActiveRecord::Base
    validates_presence_of :name, :address, :email
    validates_length_of :name, :in =&gt; 5..30
  end

  company = Company.create(:address =&gt; '123 First St.')
  company.errors.to_xml
  # =&gt;  &lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
  #     &lt;errors&gt;
  #       &lt;error&gt;Name is too short (minimum is 5 characters)&lt;/error&gt;
  #       &lt;error&gt;Name can't be blank&lt;/error&gt;
  #       &lt;error&gt;Address can't be blank&lt;/error&gt;
  #     &lt;/errors&gt;
</pre>
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M001653-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M001653-source">
<pre>
     <span class="ruby-comment cmt"># File vendor/rails/activerecord/lib/active_record/validations.rb, line 248</span>
248:     <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">to_xml</span>(<span class="ruby-identifier">options</span>={})
249:       <span class="ruby-identifier">options</span>[<span class="ruby-identifier">:root</span>] <span class="ruby-operator">||=</span> <span class="ruby-value str">&quot;errors&quot;</span>
250:       <span class="ruby-identifier">options</span>[<span class="ruby-identifier">:indent</span>] <span class="ruby-operator">||=</span> <span class="ruby-value">2</span>
251:       <span class="ruby-identifier">options</span>[<span class="ruby-identifier">:builder</span>] <span class="ruby-operator">||=</span> <span class="ruby-constant">Builder</span><span class="ruby-operator">::</span><span class="ruby-constant">XmlMarkup</span>.<span class="ruby-identifier">new</span>(<span class="ruby-identifier">:indent</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-identifier">options</span>[<span class="ruby-identifier">:indent</span>])
252: 
253:       <span class="ruby-identifier">options</span>[<span class="ruby-identifier">:builder</span>].<span class="ruby-identifier">instruct!</span> <span class="ruby-keyword kw">unless</span> <span class="ruby-identifier">options</span>.<span class="ruby-identifier">delete</span>(<span class="ruby-identifier">:skip_instruct</span>)
254:       <span class="ruby-identifier">options</span>[<span class="ruby-identifier">:builder</span>].<span class="ruby-identifier">errors</span> <span class="ruby-keyword kw">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">e</span><span class="ruby-operator">|</span>
255:         <span class="ruby-identifier">full_messages</span>.<span class="ruby-identifier">each</span> { <span class="ruby-operator">|</span><span class="ruby-identifier">msg</span><span class="ruby-operator">|</span> <span class="ruby-identifier">e</span>.<span class="ruby-identifier">error</span>(<span class="ruby-identifier">msg</span>) }
256:       <span class="ruby-keyword kw">end</span>
257:     <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>


    </div>


  </div>


<div id="validator-badges">
  <p><small><a href="http://validator.w3.org/check/referer">[Validate]</a></small></p>
</div>

</body>
</html>